package cn.shop.ylp.controll;

import cn.shop.ylp.*;
import cn.shop.ylp.error.BussinessException;
import cn.shop.ylp.error.CommonError;
import cn.shop.ylp.error.CommonReturnType;
import cn.shop.ylp.error.EmBussinessError;
import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author yulianpeng
 * @email network_u@163.com
 * Date 2020/9/12
 */
@Controller
@RequestMapping("/shopcar")
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class ShopCarC extends BaseController {
    @Autowired
    private ShopCarMapper shopCarMapper;
    @Autowired
    private  Login loginS;
    @RequestMapping(value = "/add",method = {RequestMethod.POST})
    @ResponseBody
    public CommonReturnType addProduct(@RequestParam(name = "goodsid") Integer id,
                                     @RequestParam(name = "count") Integer count,
                                     @RequestParam(name = "price") String price,
                                       @RequestParam(name = "sizesq") String sizesq,
                                     HttpServletRequest request
    ) throws BussinessException {
        User user = loginS.getUserBySession(request);
        String flag = "success";
        if(user==null){
            //未登陆
            flag = "error";
        }
        ShopCar shopCar = new ShopCar();
        shopCar.setAddDate(PubFun.cuurDate());
        shopCar.setAddTime(PubFun.cuurTime());
        shopCar.setCount(String.valueOf(count));
        shopCar.setpId(String.valueOf(id));
        shopCar.setuId(String.valueOf(user.getId()));
        shopCar.setPrice(price);
        //这个参数存规格的参数序列
        shopCar.settId(sizesq);
        int r = shopCarMapper.insertSelective(shopCar);
        if (r<=0){
            throw new BussinessException(EmBussinessError.INSERT_ERROR);
        }
        return CommonReturnType.create(null,flag);
    }
    @RequestMapping(value = "/car",method = {RequestMethod.POST})
    @ResponseBody
    public CommonReturnType getCarShop(@RequestParam(name = "userid") Integer id){
        ArrayList<ShopCar> shopCars = shopCarMapper.selectByUser(id);
        HashMap<String,ShopCar> tShopMap = new HashMap<String,ShopCar>();
        TreeSet<String> tSet = new TreeSet<>();
        for (int i = 0; i < shopCars.size(); i++) {
            ShopCar baseShop = shopCars.get(i);
            String pId = baseShop.getpId();
            String normId = baseShop.gettId();
            if (normId==null){
                normId = "";
            }
            String key = pId+"_"+normId;
            boolean tR=tSet.add(key);
            if (tR){
                //这个商品要新建
                tShopMap.put(key,baseShop);
            } else {
                //合并
                ShopCar shopCar = tShopMap.get(key);
                int tOra = Integer.parseInt(shopCar.getCount());
                int tadd = Integer.parseInt(baseShop.getCount());
                shopCar.setCount(String.valueOf(tOra+tadd));
            }
        }
        ArrayList<ShopCar> tShopC = new ArrayList<ShopCar>();
        Iterator<String> tSetTor=tSet.iterator();
        while (tSetTor.hasNext()){
            String tKey =tSetTor.next();
            tShopC.add(tShopMap.get(tKey));
        }
        //将同类合并数量返回。
        return CommonReturnType.create(tShopC);
    }
    @RequestMapping(value = "/delete",method = {RequestMethod.POST})
    @ResponseBody
    public CommonReturnType delete(
            @RequestParam(name = "userid") String uid,
            @RequestParam(name = "productid") String pid,
            @RequestParam(name = "sizesq") String sizesq

    ){
        ArrayList<ShopCar> shopCars =shopCarMapper.selectShopCars(uid,pid,sizesq);
        for (int i = 0; i < shopCars.size(); i++) {
            int shopid = shopCars.get(i).getId();
            shopCarMapper.deleteByPrimaryKey(shopid);
        }
        return  CommonReturnType.create(null);
    }

}
